<template>
  <div class="cart-container">
    <h1>购物车</h1>
    
    <div v-if="cartItems.length === 0" class="empty-cart">
      <el-empty description="购物车是空的">
        <el-button type="primary" @click="$router.push('/')">去购物</el-button>
      </el-empty>
    </div>
    
    <div v-else class="cart-content">
      <el-table :data="cartItems" style="width: 100%">
        <el-table-column label="商品" width="400">
          <template #default="{ row }">
            <div class="product-info">
              <img :src="row.image" :alt="row.name" class="product-image" />
              <span class="product-name">{{ row.name }}</span>
            </div>
          </template>
        </el-table-column>
        
        <el-table-column label="单价" width="120">
          <template #default="{ row }">
            <span class="price">¥{{ row.price.toFixed(2) }}</span>
          </template>
        </el-table-column>
        
        <el-table-column label="数量" width="150">
          <template #default="{ row }">
            <el-input-number 
              v-model="row.quantity" 
              :min="1" 
              :max="99"
              @change="updateQuantity(row.id, row.quantity)"
            />
          </template>
        </el-table-column>
        
        <el-table-column label="小计" width="120">
          <template #default="{ row }">
            <span class="subtotal">¥{{ (row.price * row.quantity).toFixed(2) }}</span>
          </template>
        </el-table-column>
        
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button type="danger" @click="removeItem(row.id)" :icon="Delete" circle />
          </template>
        </el-table-column>
      </el-table>
      
      <div class="cart-footer">
        <div class="cart-summary">
          <div class="summary-item">
            <span>商品总数:</span>
            <span>{{ cartStore.totalCount }} 件</span>
          </div>
          <div class="summary-item total">
            <span>合计:</span>
            <span class="total-amount">¥{{ cartStore.totalAmount.toFixed(2) }}</span>
          </div>
        </div>
        
        <div class="cart-actions">
          <el-button @click="$router.push('/')">继续购物</el-button>
          <el-button type="danger" @click="clearCart">清空购物车</el-button>
          <el-button type="primary" @click="checkout">结算</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useCartStore } from '../store/cartStore'
import { Delete } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const cartStore = useCartStore()

const cartItems = computed(() => cartStore.items)

const updateQuantity = (id, quantity) => {
  cartStore.updateQuantity(id, quantity)
}

const removeItem = (id) => {
  ElMessageBox.confirm('确定要从购物车中移除该商品吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    cartStore.removeFromCart(id)
    ElMessage({
      type: 'success',
      message: '商品已从购物车中移除'
    })
  }).catch(() => {})
}

const clearCart = () => {
  ElMessageBox.confirm('确定要清空购物车吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    cartStore.clearCart()
    ElMessage({
      type: 'success',
      message: '购物车已清空'
    })
  }).catch(() => {})
}

const checkout = () => {
  ElMessage({
    type: 'info',
    message: '结算功能尚未实现'
  })
}
</script>

<style scoped>
.cart-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

h1 {
  margin-bottom: 30px;
  text-align: center;
}

.empty-cart {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 50px 0;
}

.product-info {
  display: flex;
  align-items: center;
}

.product-image {
  width: 80px;
  height: 80px;
  object-fit: cover;
  margin-right: 15px;
  border-radius: 4px;
}

.product-name {
  font-weight: 500;
}

.price, .subtotal {
  font-weight: 500;
}

.subtotal {
  color: var(--el-color-danger);
}

.cart-footer {
  margin-top: 30px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.cart-summary {
  min-width: 250px;
}

.summary-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}

.total {
  font-size: 18px;
  font-weight: bold;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid #eee;
}

.total-amount {
  color: var(--el-color-danger);
}

.cart-actions {
  display: flex;
  gap: 10px;
}
</style>